package com.logicwin.aibala.services.xml;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import android.util.Xml;

import com.logicwin.aibala.model.androidSWInfo.APKListItem;
import com.logicwin.aibala.model.androidSWInfo.APKRecommend;
import com.logicwin.aibala.model.androidSWInfo.APKTotalInfo;
import com.logicwin.aibala.services.file.FileConnection;

/**
 * xml解析器
 */
public class XMLParser {
	static final String tag = "XMLParser";
	
	private static  final int LIST_NAME = 0;
	private static final int LIST_SCORE = 1;
	private static final int LIST_SIZE = 2;
	private static final int LIST_DOWNLOADTIME = 3;
	private static final int LIST_ICON = 4;
	private static final int LIST_DOWNLOADURL = 5;
	private static final int LIST_SIGN = 6;
	private static final int LIST_VERSION = 7;
	private static final int LIST_ID = 8;
	
	public static List<APKListItem> parseList(String xmlPath) {
		List<APKListItem> apkItemList = new ArrayList<APKListItem>();
		APKListItem apkItem = null;
		InputStream inputStream = null;
		
		boolean needAddRecommend = false;
		if (APKRecommend.isEmpty())
			needAddRecommend = true;
		String recommendSoftId = "";
		String recommendImage = "";
		
		// 获得XmlPullParser解析器
		XmlPullParser xmlParser = Xml.newPullParser();
		try {
			// 得到文件流，并设置编码方式
			inputStream = FileConnection.getFileStream(xmlPath);
			xmlParser.setInput(inputStream, "utf-8");
			// 获得解析到的事件类别，这里有开始文档，结束文档，开始标签，结束标签，文本等等事件。
			int evtType = xmlParser.getEventType();
			// 一直循环，直到文档结束
			while (evtType != XmlPullParser.END_DOCUMENT) {
				switch (evtType) {
				case XmlPullParser.START_TAG:
					String tag = xmlParser.getName();
					// 如果是software标签开始，则说明需要实例化对象了
					if (tag.equalsIgnoreCase("software")) {
						apkItem = new APKListItem();
						// 取出software标签中的一些属性值
						apkItem.setAPKName(xmlParser.getAttributeValue(LIST_NAME));
						apkItem.setAPKSize(xmlParser.getAttributeValue(LIST_SIZE));
						apkItem.setRefStarCount(Integer.parseInt(xmlParser.getAttributeValue(LIST_SCORE)));
						apkItem.setDownloadCount(xmlParser.getAttributeValue(LIST_DOWNLOADTIME));
						apkItem.setIconURL(xmlParser.getAttributeValue(LIST_ICON));
						apkItem.setAPKAddress(xmlParser.getAttributeValue(LIST_DOWNLOADURL).replace("&amp;", "&"));
						apkItem.setVersion(xmlParser.getAttributeValue(LIST_VERSION));
						apkItem.setID(Integer.parseInt(xmlParser.getAttributeValue(LIST_ID)));
						
					}
					else if (tag.equalsIgnoreCase("recommend")&&needAddRecommend){
						recommendSoftId = xmlParser.getAttributeValue(1);
						recommendImage = xmlParser.getAttributeValue(0);
					}
					break;

				case XmlPullParser.END_TAG:
					// 如果遇到software标签结束，则把software对象添加进集合中
					if (xmlParser.getName().equalsIgnoreCase("software")
							&& apkItem != null) {
						apkItemList.add(apkItem);
						apkItem = null;
					}
					else if (xmlParser.getName().equalsIgnoreCase("recommend")&&needAddRecommend) {
						APKRecommend.addRecommend(recommendSoftId, recommendImage);
					}
					break;
				default:
					break;
				}
				// 如果xml没有结束，则导航到下一个software节点
				evtType = xmlParser.next();
			}
		} catch (XmlPullParserException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		return apkItemList;
	}
	
	public static APKTotalInfo parseDetail(String xmlPath) {
		
		APKListItem apkItem = new APKListItem();
		APKTotalInfo apkTotalInfo = new APKTotalInfo(apkItem);

		InputStream inputStream = null;
		// 获得XmlPullParser解析器
		XmlPullParser xmlParser = Xml.newPullParser();
		try {
			// 得到文件流，并设置编码方式
			inputStream = FileConnection.getFileStream(xmlPath);
			xmlParser.setInput(inputStream, "utf-8");
			// 获得解析到的事件类别，这里有开始文档，结束文档，开始标签，结束标签，文本等等事件。
			int evtType = xmlParser.getEventType();
			// 一直循环，直到文档结束
			while (evtType != XmlPullParser.END_DOCUMENT) {
				switch (evtType) {
				case XmlPullParser.START_TAG:
					String tag = xmlParser.getName();
					if (tag.equalsIgnoreCase("name")) {
						apkItem.setAPKName(xmlParser.nextText());	
					} 
					else if (tag.equalsIgnoreCase("size")) {
						apkItem.setAPKSize(xmlParser.nextText());	
					} 
					else if (tag.equalsIgnoreCase("downloadUrl")) {
						apkItem.setAPKAddress(xmlParser.nextText().replace("&amp;", "&"));	
					} 
					else if (tag.equalsIgnoreCase("version")) {
						apkItem.setVersion(xmlParser.nextText());	
					} 
					else if (tag.equalsIgnoreCase("icon")) {
						apkItem.setIconURL(xmlParser.nextText());	
					} 
					else if (tag.equalsIgnoreCase("score")) {
						apkItem.setRefStarCount(Integer.parseInt(xmlParser.nextText()));	
					} 
					else if (tag.equalsIgnoreCase("download")) {
						apkItem.setDownloadCount(xmlParser.nextText());	
					} 
					else if (tag.equalsIgnoreCase("publishDate")) {
						apkTotalInfo.getDetailDesc().setPublishDate(xmlParser.nextText());
					} 
					else if (tag.equalsIgnoreCase("description")) {
						apkTotalInfo.getDetailDesc().setDescription(xmlParser.nextText());
					} 
					else if (tag.equalsIgnoreCase("samplePic")) {
						apkTotalInfo.getDetailDesc().addSamplePic(xmlParser.nextText());
					} 
					else if (tag.equalsIgnoreCase("usageComment")) {
						apkTotalInfo.getDetailDesc().setUsageComments(xmlParser.nextText());
					} 
					else if (tag.equalsIgnoreCase("producer")) {
						apkTotalInfo.getDetailDesc().setProducer(xmlParser.nextText());
					} 
					else if (tag.equalsIgnoreCase("platform")) {
						apkTotalInfo.getDetailDesc().setPlatform(xmlParser.nextText());
					} 
					else if (tag.equalsIgnoreCase("shareGuy")) {
						apkTotalInfo.getDetailDesc().setShareGuy(xmlParser.nextText());
					} 
					break;

				case XmlPullParser.END_TAG:
					break;
				default:
					break;
				}
				// 如果xml没有结束，则导航到下一个software节点
				evtType = xmlParser.next();
			}
		} catch (XmlPullParserException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		return apkTotalInfo;
	}
}
